package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.ExchangePacket;
import cc.yugu.yhdw.common.domain.hdw.ExchangePacketOrder;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.hdw.AgencyMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangePacketMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * 换电套餐业务
 * 
 * @Date:Created in 2018/7/9 13:49
 * @Author:
 * @Modified By: zjn 2018/7/9 13:49
 */ 
@Service
public class ExchangePacketService extends AbstractService {

    @Autowired
    ExchangePacketMapper exchangePacketMapper;
    @Autowired
    AgencyMapper agencyMapper;
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  13:49
    */ 
    public Page findPage(ExchangePacket search) {
        /*
         查询套餐订单数量
         1.代理商
                --如果没有设置，查询运营商的设置
                --如果有设置，显示自己的设置
         2.运营商
                --必须设置，自己的设置的代理商id属性为null
        */
        Page page = search.buildPage();
        List<ExchangePacket> list = null;
        int count = exchangePacketMapper.findPageCount(search);
        // 代理商 -- 这个业务逻辑要重新设计
        if (search.getAgencyId() != null && count <= 0) {
            // 代理商没有设置换电套餐，自动使用上级设置的换电套餐
            // 查询这个代理商的上级运营商
            search.setAgentId(agencyMapper.findInfo(search.getAgencyId()).getAgentId());
            search.setAgencyId(null);
            page.setTotalItems(exchangePacketMapper.findPageCounts(search));
            search.setBeginIndex(page.getOffset());
            list = exchangePacketMapper.findPageResults(search);
        } else {
            page.setTotalItems(exchangePacketMapper.findPageCount(search));
            search.setBeginIndex(page.getOffset());
            list = exchangePacketMapper.findPageResult(search);
        }

        for (ExchangePacket exchangePacket : list) {
            Integer agentId = exchangePacket.getAgentId();
            Integer agencyId = exchangePacket.getAgencyId();
            // 设置运营商名称
            if (agentId != null) {
                if (findAgentInfo(agentId) != null) {
                    // 非常耗费时间
                    exchangePacket.setAgentName(findAgentInfo(agentId).getAgentName());
                }
            }
            // 设置代理商名称
            if (agencyId != null) {
                if (findAgency(agencyId) != null) {
                    exchangePacket.setAgencyName(findAgency(agencyId).getAgencyName());
                }
            }
        }
        page.setResult(list);
        return page;
    }

    public ExchangePacket find(Long id) {
        return exchangePacketMapper.find(id);
    }
    /**
    * 按主键删除
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  13:49
    */ 
    public ExtResult delete(Long id) {
        int total = exchangePacketMapper.delete(id);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }
    /**
    * 创建运营商和代理商套餐信息
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.DataResult
    * @Author By: zjn 2018/7/9  13:49
    */ 
    public DataResult create(ExchangePacket entity) {
        entity.setCreateTime(new Date());
        if (entity.getType() == null){
            // 设置换电套餐类型为 按次数
            entity.setType(ExchangePacket.Type.CODE_1.getValue());
        }else{
            if(entity.getType() == ExchangePacket.Type.CODE_1.getValue()){
                entity.setDayCount(0);
            }else{
                entity.setNumber(0);
            }
        }
        int total = exchangePacketMapper.insert(entity);
        if (total == 0) {
            return DataResult.failResult("操作失败！", "");
        }
        return DataResult.successResult(entity.getId());
    }
    /**
    * 更新
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  13:58
    */ 
    public ExtResult update(ExchangePacket entity) {
        if (entity.getType()==null){
            entity.setType(ExchangePacket.Type.CODE_1.getValue());
        }else{
            if(entity.getType()==ExchangePacket.Type.CODE_1.getValue()){
                entity.setDayCount(0);
            }else{
                entity.setNumber(0);
            }
        }
        int total = exchangePacketMapper.update(entity);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }
}
